# 通用工具

import re
import os
from PySide2.QtGui import QClipboard
from PySide2.QtCore import QFileInfo
from urllib.parse import unquote  # 路径解码
from PySide2.QtQml import QJSValue

Clipboard = QClipboard()  # 剪贴板

ImageSuf = [  # 合法图片后缀
    ".jpg",
    ".jpe",
    ".jpeg",
    ".jfif",
    ".png",
    ".webp",
    ".bmp",
    ".tif",
    ".tiff",
]


# 传入文件名，检测是否含非法字符。没问题返回True
def allowedFileName(fn):
    pattern = r'[\\/:*?"<>|]'
    if re.search(pattern, fn):
        return False  # 转布尔值
    else:
        return True


def isImg(path):  # 路径是图片返回true
    return os.path.splitext(path)[-1].lower() in ImageSuf


# 传入路径列表，在路径中搜索图片。isRecurrence=True时递归搜索。
def findImages(paths, isRecurrence):
    if type(paths) == QJSValue:
        paths = paths.toVariant()
    if type(paths) != list:
        print(f"[Error] findImages 传入：{paths}, {type(paths)}")
        return []
    # isRecurrence 递归读取
    imgPaths = []
    for p in paths:
        if os.path.isfile(p) and isImg(p):  # 是文件，直接判断
            imgPaths.append(os.path.abspath(p))
        elif os.path.isdir(p):  # 是路径
            if isRecurrence:  # 需要递归
                for root, dirs, files in os.walk(p):
                    print("dirs: ", dirs)
                    for file in files:
                        if isImg(file):  # 收集子文件
                            imgPaths.append(
                                os.path.abspath(os.path.join(root, file))
                            )  # 将路径转换为绝对路径
            else:  # 不递归读取子文件夹
                for file in os.listdir(p):
                    if os.path.isfile(os.path.join(p, file)) and isImg(file):
                        imgPaths.append(os.path.abspath(os.path.join(p, file)))
    for i, p in enumerate(imgPaths):  # 规范化正斜杠
        imgPaths[i] = p.replace("\\", "/")
    return imgPaths


# 复制文本到剪贴板
def copyText(text):
    Clipboard.setText(text)


# QUrl列表 转 String列表
def QUrl2String(urls):
    resList = []
    for url in urls:
        if url.isLocalFile():
            u = unquote(url.toLocalFile())  # 解码路径
            if QFileInfo(u).exists():  # 检查路径是否真的存在
                resList.append(u)
    return resList
